package com.souku.finance.application.ui.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.CurrentBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.EdittextListenUtil;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.PayUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.ButtonWait;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/25.
 */

public class CurrentOutActivity extends BaseActivity implements View.OnClickListener {
    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected EditText etMoney;
    protected TextView tvMoney;
    protected ButtonWait btnOut;
    private CurrentBean bean;

    @Override
    public void internet() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bean = getIntent().getParcelableExtra(Constant.Default);
        super.setContentView(R.layout.act_currentout);
        initView();
        double money = Double.parseDouble(MyTextUtil.setMoneyToElement(bean.getCurrentDealLoadDO().getMoney()));
        double dayWithdrawAmountRemain = Double.parseDouble(MyTextUtil.setMoneyToElement(bean.getDayWithdrawAmountRemain()));
        EdittextListenUtil.setDouble(etMoney, dayWithdrawAmountRemain < money ? dayWithdrawAmountRemain : money);
        etMoney.setHint("当日还可转出" + dayWithdrawAmountRemain + "元");
        tvMoney.setText(money + "元");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.btn_out) {
            String money = etMoney.getText().toString().trim();
            if (MyTextUtil.isEmpty(money) || Double.parseDouble(money) == 0) {
                ToastUtil.show("请输入转出金额");
                return;
            }
            btnOut.setEnabled(false);
            withDraw(PayUtil.getEoIntMoney(money) + "");
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(CurrentOutActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        etMoney = (EditText) findViewById(R.id.et_money);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        btnOut = (ButtonWait) findViewById(R.id.btn_out);
        btnOut.setOnClickListener(CurrentOutActivity.this);
    }

    //活期转至余额
    private void withDraw(String money) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("money", money);
        RequestManager.getInstance().post(UrlConstact.WITHDRAW, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    MineFragment.isChange = true;
                    ToastUtil.show("转出成功");
                    finish();
                } else {
                    btnOut.setEnabled(true);
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                btnOut.setEnabled(true);
                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }
}
